package com.guaoran.source.mybatis.session;

import com.guaoran.source.mybatis.config.Configuration;
import com.guaoran.source.mybatis.config.MapperRegistory;
import com.guaoran.source.mybatis.execute.Executor;
import com.guaoran.source.mybatis.mapper.MapperProxy;
import lombok.Data;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
import java.sql.SQLException;

/**
 * @author : 孤傲然
 * @Description :
 * @date :2018/5/25 14:09
 */
@Data
public class SqlSession {
    private Configuration configuration;
    private Executor executor;

    public SqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    public <T> T getMapper(Class<T> clazz){
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new MapperProxy(this,clazz));
    }

    public <T> T selectOne(MapperRegistory.MapperData mapperData, Object parameter) throws SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return executor.selectOne(mapperData,parameter);
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public Executor getExecutor() {
        return executor;
    }

    public void setExecutor(Executor executor) {
        this.executor = executor;
    }
}
